package com.glsc.ngateway.flowable.controller;

import com.glsc.ngateway.common.api.flowable.resp.FlowableResponse;
import com.glsc.ngateway.common.base.domain.mysql.flowable.FlowableEhrDeptinfo;
import com.glsc.ngateway.common.base.domain.mysql.flowable.FlowableEhrEmp;
import com.glsc.ngateway.flowable.service.FlowableEhrDeptinfoService;
import com.glsc.ngateway.flowable.service.FlowableEhrEmpService;
import io.swagger.annotations.Api;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Deprecated
@RestController
@RequestMapping(value = "/ehr")
@Api(value = "flowable流程ehr类", tags = {"flowable流程ehr类"})
public class EhrController {

    private static Logger logger = LoggerFactory.getLogger(EhrController.class);

    @Autowired
    FlowableEhrEmpService flowableEhrEmpService;
    @Autowired
    FlowableEhrDeptinfoService flowableEhrDeptinfoService;

    @RequestMapping(method = RequestMethod.GET, value = "/getUserInfo")
    public FlowableResponse getUserInfo(@RequestParam("userId") String userId) {
        FlowableEhrEmp emp = flowableEhrEmpService.getUserInfo(userId);
        return FlowableResponse.success(emp);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/getUserList")
    public FlowableResponse getUserList(@RequestParam(value = "department", required = false) String department) {
        List<FlowableEhrEmp> emplist = flowableEhrEmpService.getUserList(department);
        return FlowableResponse.success(emplist);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/getUserListByBadge")
    public FlowableResponse<List<FlowableEhrEmp>> getUserList(@RequestParam(value = "department", required = false) List<String> badge) {
        List<FlowableEhrEmp> ehrlist = flowableEhrEmpService.findAllByBadgeIn(badge);
        if (CollectionUtils.isNotEmpty(ehrlist)) {
            Map<Integer, FlowableEhrDeptinfo> depMap = flowableEhrDeptinfoService.getDepartmentList(ehrlist.stream().map(FlowableEhrEmp::getDepid).collect(Collectors.toList()))
                    .stream().collect(Collectors.toMap(FlowableEhrDeptinfo::getDepid, o -> o));
            ehrlist.forEach(ehr -> ehr.setDeptinfo(depMap.get(ehr.getDepid())));
        }
        return FlowableResponse.success(ehrlist);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/getDepartmentList")
    public FlowableResponse getDepartmentList() {
        List<FlowableEhrDeptinfo> deplist = flowableEhrDeptinfoService.getDepartmentList();
        return FlowableResponse.success(deplist);
    }
}
